home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / externaltools / library.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  14.9 KB  |  456 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import locale
  7. import platform
  8.  
  9. class Singleton(object):
  10.     _instance = None
  11.     
  12.     def __new__(cls, *args, **kwargs):
  13.         if not cls._instance:
  14.             cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
  15.             cls._instance.__init_once__()
  16.         
  17.         return cls._instance
  18.  
  19.  
  20.  
  21. class ToolLibrary(Singleton):
  22.     
  23.     def __init_once__(self):
  24.         self.locations = []
  25.  
  26.     
  27.     def set_locations(self, datadir):
  28.         self.locations = []
  29.         if platform.platform() != 'Windows':
  30.             for d in self.get_xdg_data_dirs():
  31.                 self.locations.append(os.path.join(d, 'gedit-2/plugins/tools'))
  32.             
  33.         
  34.         self.locations.append(datadir)
  35.         if platform.platform() == 'Windows':
  36.             self.locations.insert(0, os.path.expanduser('~/gedit/tools'))
  37.         else:
  38.             self.locations.insert(0, os.path.expanduser('~/.gnome2/gedit/tools'))
  39.         if not os.path.isdir(self.locations[0]):
  40.             os.makedirs(self.locations[0])
  41.             self.tree = ToolDirectory(self, '')
  42.             self.import_old_xml_store()
  43.         else:
  44.             self.tree = ToolDirectory(self, '')
  45.  
  46.     
  47.     def get_xdg_data_dirs(self):
  48.         dirs = os.getenv('XDG_DATA_DIRS')
  49.         if dirs:
  50.             dirs = dirs.split(os.pathsep)
  51.         else:
  52.             dirs = ('/usr/local/share', '/usr/share')
  53.         return dirs
  54.  
  55.     
  56.     def import_old_xml_store(self):
  57.         import ElementTree as et
  58.         filename = os.path.expanduser('~/.gnome2/gedit/gedit-tools.xml')
  59.         if not os.path.isfile(filename):
  60.             return None
  61.         print 'External tools: importing old tools into the new store...'
  62.         xtree = et.parse(filename)
  63.         xroot = xtree.getroot()
  64.         for xtool in xroot:
  65.             for i in self.tree.tools:
  66.                 if i.name == xtool.get('label'):
  67.                     tool = i
  68.                     break
  69.                     continue
  70.             else:
  71.                 tool = Tool(self.tree)
  72.                 tool.name = xtool.get('label')
  73.                 self.tree.tools.append(tool)
  74.             tool.comment = xtool.get('description')
  75.             tool.shortcut = xtool.get('accelerator')
  76.             tool.applicability = xtool.get('applicability')
  77.             tool.output = xtool.get('output')
  78.             tool.input = xtool.get('input')
  79.             tool.save_with_script(xtool.text)
  80.         
  81.  
  82.     
  83.     def get_full_path(self, path, mode = 'r', system = True, local = True):
  84.         if not system and local:
  85.             raise AssertionError
  86.         if path is None:
  87.             return None
  88.         if mode == 'r':
  89.             if system and local:
  90.                 locations = self.locations
  91.             elif local and not system:
  92.                 locations = [
  93.                     self.locations[0]]
  94.             elif system and not local:
  95.                 locations = self.locations[1:]
  96.             else:
  97.                 raise ValueError("system and local can't be both set to False")
  98.         return None
  99.         mode == 'r'
  100.         path = os.path.join(self.locations[0], path)
  101.         dirname = os.path.dirname(path)
  102.         return path
  103.  
  104.  
  105.  
  106. class ToolDirectory(object):
  107.     
  108.     def __init__(self, parent, dirname):
  109.         super(ToolDirectory, self).__init__()
  110.         self.subdirs = list()
  111.         self.tools = list()
  112.         if isinstance(parent, ToolDirectory):
  113.             self.parent = parent
  114.             self.library = parent.library
  115.         else:
  116.             self.parent = None
  117.             self.library = parent
  118.         self.dirname = dirname
  119.         self._load()
  120.  
  121.     
  122.     def listdir(self):
  123.         elements = dict()
  124.         for l in self.library.locations:
  125.             d = os.path.join(l, self.dirname)
  126.             if not os.path.isdir(d):
  127.                 continue
  128.             
  129.             for i in os.listdir(d):
  130.                 elements[i] = None
  131.             
  132.         
  133.         keys = elements.keys()
  134.         keys.sort()
  135.         return keys
  136.  
  137.     
  138.     def _load(self):
  139.         for p in self.listdir():
  140.             path = os.path.join(self.dirname, p)
  141.             full_path = self.library.get_full_path(path)
  142.             if os.path.isdir(full_path):
  143.                 self.subdirs.append(ToolDirectory(self, p))
  144.                 continue
  145.             if os.path.isfile(full_path) and os.access(full_path, os.X_OK):
  146.                 self.tools.append(Tool(self, p))
  147.                 continue
  148.         
  149.  
  150.     
  151.     def get_path(self):
  152.         if self.parent is None:
  153.             return self.dirname
  154.         return os.path.join(self.parent.get_path(), self.dirname)
  155.  
  156.     path = property(get_path)
  157.     
  158.     def get_name(self):
  159.         return os.path.basename(self.dirname)
  160.  
  161.     name = property(get_name)
  162.     
  163.     def delete_tool(self, tool):
  164.         if tool in self.tools:
  165.             path = tool.get_path()
  166.             if path is not None:
  167.                 filename = os.path.join(self.library.locations[0], path)
  168.                 if os.path.isfile(filename):
  169.                     os.unlink(filename)
  170.                 
  171.             
  172.             self.tools.remove(tool)
  173.             return True
  174.         return False
  175.  
  176.     
  177.     def revert_tool(self, tool):
  178.         filename = os.path.join(self.library.locations[0], tool.get_path())
  179.         if tool in self.tools and os.path.isfile(filename):
  180.             os.unlink(filename)
  181.             tool._load()
  182.             return True
  183.         return False
  184.  
  185.  
  186.  
  187. class Tool(object):
  188.     RE_KEY = re.compile('^([a-zA-Z_][a-zA-Z0-9_.\\-]*)(\\[([a-zA-Z_@]+)\\])?$')
  189.     
  190.     def __init__(self, parent, filename = None):
  191.         super(Tool, self).__init__()
  192.         self.parent = parent
  193.         self.library = parent.library
  194.         self.filename = filename
  195.         self.changed = False
  196.         self._properties = dict()
  197.         self._load()
  198.  
  199.     
  200.     def _load(self):
  201.         if self.filename is None:
  202.             return None
  203.         filename = self.library.get_full_path(self.get_path())
  204.         if filename is None:
  205.             return None
  206.         fp = file(filename, 'r', 1)
  207.         in_block = False
  208.         lang = locale.getlocale(locale.LC_MESSAGES)[0]
  209.         for line in fp:
  210.             if line.startswith('##') or line.startswith('# #'):
  211.                 continue
  212.             
  213.             if not line.startswith('# '):
  214.                 break
  215.             
  216.             
  217.             try:
  218.                 (key, value) = [ i.strip() for i in line[2:].split('=', 1) ]
  219.                 m = self.RE_KEY.match(key)
  220.                 if m.group(3) is None:
  221.                     if m.group(0) not in self._properties:
  222.                         self._properties[m.group(1)] = value
  223.                     
  224.                 elif lang is not None and lang.startswith(m.group(3)):
  225.                     self._properties[m.group(1)] = value
  226.             continue
  227.             except ValueError:
  228.                 break
  229.                 continue
  230.             
  231.  
  232.         
  233.         fp.close()
  234.         self.changed = False
  235.  
  236.     
  237.     def _set_property_if_changed(self, key, value):
  238.         if value != self._properties.get(key):
  239.             self._properties[key] = value
  240.             self.changed = True
  241.         
  242.  
  243.     
  244.     def is_global(self):
  245.         return self.library.get_full_path(self.get_path(), local = False) is not None
  246.  
  247.     
  248.     def is_local(self):
  249.         return self.library.get_full_path(self.get_path(), system = False) is not None
  250.  
  251.     
  252.     def is_global(self):
  253.         return self.library.get_full_path(self.get_path(), local = False) is not None
  254.  
  255.     
  256.     def get_path(self):
  257.         if self.filename is not None:
  258.             return os.path.join(self.parent.get_path(), self.filename)
  259.         return None
  260.  
  261.     path = property(get_path)
  262.     
  263.     def get_command(self):
  264.         return self.library.get_full_path(self.get_path())
  265.  
  266.     command = property(get_command)
  267.     
  268.     def get_applicability(self):
  269.         applicability = self._properties.get('Applicability')
  270.         if applicability:
  271.             return applicability
  272.         return 'all'
  273.  
  274.     
  275.     def set_applicability(self, value):
  276.         self._set_property_if_changed('Applicability', value)
  277.  
  278.     applicability = property(get_applicability, set_applicability)
  279.     
  280.     def get_name(self):
  281.         name = self._properties.get('Name')
  282.         if name:
  283.             return name
  284.         return os.path.basename(self.filename)
  285.  
  286.     
  287.     def set_name(self, value):
  288.         self._set_property_if_changed('Name', value)
  289.  
  290.     name = property(get_name, set_name)
  291.     
  292.     def get_shortcut(self):
  293.         shortcut = self._properties.get('Shortcut')
  294.         if shortcut:
  295.             return shortcut
  296.  
  297.     
  298.     def set_shortcut(self, value):
  299.         self._set_property_if_changed('Shortcut', value)
  300.  
  301.     shortcut = property(get_shortcut, set_shortcut)
  302.     
  303.     def get_comment(self):
  304.         comment = self._properties.get('Comment')
  305.         if comment:
  306.             return comment
  307.         return self.filename
  308.  
  309.     
  310.     def set_comment(self, value):
  311.         self._set_property_if_changed('Comment', value)
  312.  
  313.     comment = property(get_comment, set_comment)
  314.     
  315.     def get_input(self):
  316.         input = self._properties.get('Input')
  317.         if input:
  318.             return input
  319.         return 'nothing'
  320.  
  321.     
  322.     def set_input(self, value):
  323.         self._set_property_if_changed('Input', value)
  324.  
  325.     input = property(get_input, set_input)
  326.     
  327.     def get_output(self):
  328.         output = self._properties.get('Output')
  329.         if output:
  330.             return output
  331.         return 'output-panel'
  332.  
  333.     
  334.     def set_output(self, value):
  335.         self._set_property_if_changed('Output', value)
  336.  
  337.     output = property(get_output, set_output)
  338.     
  339.     def get_script(self):
  340.         if self.filename is None:
  341.             return []
  342.         filename = self.library.get_full_path(self.get_path())
  343.         if filename is None:
  344.             return []
  345.         fp = open(filename, 'r', 1)
  346.         lines = list()
  347.         for line in fp:
  348.             lines.append(line)
  349.         
  350.         for line in fp:
  351.             if line.startswith('##'):
  352.                 continue
  353.             
  354.             if not line.startswith('# ') and '=' in line:
  355.                 if line.strip() != '':
  356.                     lines.append(line)
  357.                 
  358.                 break
  359.                 continue
  360.         
  361.         for line in fp:
  362.             lines.append(line)
  363.         
  364.         fp.close()
  365.         return lines
  366.  
  367.     
  368.     def _dump_properties(self):
  369.         lines = [
  370.             '# [Gedit Tool]']
  371.         for item in self._properties.iteritems():
  372.             if item[1] is not None:
  373.                 lines.append('# %s=%s' % item)
  374.                 continue
  375.         
  376.         return '\n'.join(lines) + '\n'
  377.  
  378.     
  379.     def save_with_script(self, script):
  380.         filename = self.library.get_full_path(self.filename, 'w')
  381.         fp = open(filename, 'w', 1)
  382.         
  383.         try:
  384.             script = iter(script)
  385.             line = script.next()
  386.             if line.startswith('#!'):
  387.                 fp.write(line)
  388.                 line = script.next()
  389.             
  390.             if '-*-' in line:
  391.                 fp.write(line)
  392.                 line = script.next()
  393.             
  394.             if 'ex:' in line and 'vi:' in line or 'vim:' in line:
  395.                 fp.write(line)
  396.                 line = script.next()
  397.             
  398.             if line.strip() == '':
  399.                 fp.write(line)
  400.                 line = script.next()
  401.             
  402.             fp.write(self._dump_properties())
  403.             fp.write('\n')
  404.             while True:
  405.                 fp.write(line)
  406.                 line = script.next()
  407.         except StopIteration:
  408.             pass
  409.  
  410.         fp.close()
  411.         os.chmod(filename, 488)
  412.         self.changed = False
  413.  
  414.     
  415.     def save(self):
  416.         if self.changed:
  417.             self.save_with_script(self.get_script())
  418.         
  419.  
  420.     
  421.     def autoset_filename(self):
  422.         if self.filename is not None:
  423.             return None
  424.         dirname = self.parent.path
  425.         if dirname != '':
  426.             dirname += os.path.sep
  427.         
  428.         basename = self.name.lower().replace(' ', '-').replace('/', '-')
  429.         if self.library.get_full_path(dirname + basename):
  430.             i = 2
  431.             while self.library.get_full_path(dirname + '%s-%d' % (basename, i)):
  432.                 i += 1
  433.             basename = '%s-%d' % (basename, i)
  434.         
  435.         self.filename = basename
  436.  
  437.  
  438. if __name__ == '__main__':
  439.     library = ToolLibrary()
  440.     
  441.     def print_tool(t, indent):
  442.         print indent * '  ' + '%s: %s' % (t.filename, t.name)
  443.  
  444.     
  445.     def print_dir(d, indent):
  446.         print indent * '  ' + d.dirname + '/'
  447.         for i in d.subdirs:
  448.             print_dir(i, indent + 1)
  449.         
  450.         for i in d.tools:
  451.             print_tool(i, indent + 1)
  452.         
  453.  
  454.     print_dir(library.tree, 0)
  455.  
  456.